package thread.lock;

/**
 * @author 真香
 * @create 2021-06-04 18:19
 */

/**
 * 死锁
 */
public class DeadLock {

    public static void main(String[] args) {

        new Thread(new MakeUp(0,"小微")).start();
        new Thread(new MakeUp(1,"小雅")).start();

    }
}

// 口红
class Lipstick {


}


// 镜子
class Mirror {


}

// 化妆
class MakeUp implements Runnable{


    static Lipstick lipstick = new Lipstick();
    static Mirror mirror = new Mirror();

    // 选择镜子还是口红
    private int chechk ;

    // 化妆的人
    private String name;


    public MakeUp(int chechk, String name) {
        this.chechk = chechk;
        this.name = name;
    }

    @Override
    public void run() {
        try {
            // 调用化妆
            makeUp();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void makeUp () throws InterruptedException {
        if (chechk == 0) {

            synchronized (lipstick) {
                System.out.println(this.name+"获得了口红的锁");
                Thread.sleep(1000);
                // 一秒钟后想拿镜子

            }
            synchronized (mirror) {
                System.out.println(this.name+"获得了镜子的锁");
            }
        } else {
            synchronized (mirror) {
                System.out.println(this.name+"获得了镜子的锁");
                Thread.sleep(2000);

            }

            synchronized (lipstick) {
                System.out.println(this.name+"获得了口红的锁");
            }

        }

    }
}